package cn.zaichi.edu.excelcommon.utils;

import java.io.IOException;
import java.security.GeneralSecurityException;

import cn.zaichi.edu.excelcommon.entity.BaseExportParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class ExportWorkbookUtil {
    /**
     * 获取 工作簿
     * @param param
     * @return
     * @throws IOException
     * @throws GeneralSecurityException
     */
    public static Workbook getWorkbook(BaseExportParam param) throws IOException, GeneralSecurityException {
        if(param.getTempPath()!=null){
            return ImportWorkbookUtil.onStart(param.getTempPath(),param.getTempPass());
        }else{
            return new HSSFWorkbook();
        }
    }

    /**
     * 获取sheet
     * @param param
     * @param workbook
     * @return
     */
    public static HSSFSheet getSheet(BaseExportParam param,Workbook workbook){
        HSSFSheet sheet;
        if(param.getTempPath()!=null){

             sheet = (HSSFSheet) workbook.getSheetAt(param.getSheetNum());

        }else{
            sheet= (HSSFSheet) workbook.createSheet(param.getSheetName());
        }
        if(StringUtils.isNotEmpty(param.getTempPass())){
            sheet.protectSheet(param.getTempPass());
        }
        return sheet;
    }
}
